import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 14229 on 2020/2/21.
 */
public class Test07 {
    public static void main(String[] args) throws DocumentException {

        List<Emp> emps = getEmps();
        List<Dept> Depts = getDepts();
        List<Map<String, String>> evg = getEvg(getDepts());
        for (Map<String, String>  masg : evg) {
            System.out.println(masg);
        }
    }
    public static List<Map<String,String>> getEvg(List<Dept> list) throws DocumentException {
        List<Emp> emps = getEmps();
        List<Map<String,String>> lsit = new ArrayList<>();
        for (Dept dept : list) {
            Map<String,String> map = new HashMap<>();
            int count = 0;
            int sum = 0;
            for (Emp emp : emps) {
                if (dept.getDeptid().equals(emp.getDeptid())){
                    count++;
                    sum+=emp.getEmpsalary();
                }
            }
            map.put(dept.getDeptname(),(sum/count)+"");
            lsit.add(map);
        }
        return lsit;
    }
    public static List<Emp> getEmps() throws DocumentException {
        List<Emp> list = new ArrayList<>();
        SAXReader sr = new SAXReader();
        Document read = sr.read("emp.xml");
        Element rootElement = read.getRootElement();
        List<Element> elements = rootElement.elements();
        for (Element element : elements) {
            String s = element.elementText("emp-name");
            String s1 = element.elementText("emp-id");
            String s2 = element.elementText("dept-id");
            String s3 = element.elementText("emp-salary");
            list.add(new Emp(s,s1,s2,Integer.parseInt(s3)));
        }
        return list;
    }
    public static List<Dept> getDepts() throws DocumentException {
        List<Dept> list = new ArrayList<>();
        SAXReader sr = new SAXReader();
        Document read = sr.read("dept.xml");
        Element rootElement = read.getRootElement();
        List<Element> elements = rootElement.elements();
        for (Element element : elements) {
            String s = element.elementText("dept-name");
            String s1 = element.elementText("dept-id");
            list.add(new Dept(s,s1));
        }
        return list;

    }
}
